﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace Cobit5.BIZ.Cobit
{
    public class Activity
    {
        #region Model
        private string _practiceid;
        private int _sequence;
        private string _activity;
        private string _activitychn;
        private string _activityowner;
        private string _activityreviewer;
        private int? _frequency;
        /// <summary>
        /// 
        /// </summary>
        public string practiceID
        {
            set { _practiceid = value; }
            get { return _practiceid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int sequence
        {
            set { _sequence = value; }
            get { return _sequence; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string activity
        {
            set { _activity = value; }
            get { return _activity; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string activityCHN
        {
            set { _activitychn = value; }
            get { return _activitychn; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string activityOwner
        {
            set { _activityowner = value; }
            get { return _activityowner; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string activityReviewer
        {
            set { _activityreviewer = value; }
            get { return _activityreviewer; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int? frequency
        {
            set { _frequency = value; }
            get { return _frequency; }
        }
        #endregion Model

        #region ActivityFuntion

        /// <summary>
        /// Initialize
        /// </summary>
        public Activity(string in_id,int in_sequence)
        {
            if (Activity.ExistActivity(in_id, in_sequence) == 0)
                return;

            Model.Activities myActivity = new Model.Activities();
            DAL.Activities optActivity = new DAL.Activities();
            myActivity = optActivity.GetModel(in_id, in_sequence);

            this._practiceid = myActivity.practiceID;
            this._sequence = myActivity.sequence;
            this._activity = myActivity.activity;
            this._activitychn = myActivity.activityCHN;
            this._activityowner = myActivity.activityOwner;
            this._activityreviewer = myActivity.activityReviewer;
            this._frequency = myActivity.frequency;
        }

        /// <summary>
        /// Count the number of Activity
        /// </summary>
        public static int CountAllProcess()
        {
            DAL.Activities optActivity = new DAL.Activities();
            return optActivity.GetRecordCount("1=1");
        }

        /// <summary>
        /// List all the Activity in pages
        /// </summary>
        public static List<Activity> GetAllActivity(string in_orderby, int in_startIndex, int in_endIndex)
        {
            DAL.Activities optActivity = new DAL.Activities();
            DataSet ds = optActivity.GetListByPage("1=1", in_orderby, in_startIndex, in_endIndex);

            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<Activity> returnListActivity = new List<Activity>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Activity newActivity = new Activity(ds.Tables[0].Rows[i]["practiceID"].ToString(), int.Parse(ds.Tables[0].Rows[i]["sequence"].ToString()));
                returnListActivity.Add(newActivity);
            }
            
            return returnListActivity;
        }

        /// <summary>
        /// Check if the Activity exists
        /// Return  1:Exist;
        ///         0:Not Exist;
        /// </summary>
        public static int ExistActivity(string in_practiceID, int in_sequence)
        {
            DAL.Activities optActivity = new DAL.Activities();
            if (!optActivity.Exists(in_practiceID, in_sequence))
                return 0;
            else return 1;
        }

        /// <summary>
        /// Create a new Activity
        /// Return  1.Success
        ///         2.Id already existed
        ///         3.DAL Error
        /// </summary>
        public static int CreateActivity(string in_practiceID, int in_sequence, string in_activity, string in_activityCHN, string in_activityOwner, string in_activityReviewer, int in_frequency)
        {
            if (Activity.ExistActivity(in_practiceID, in_sequence) == 1)
                return 2;

            Model.Activities myActivity = new Model.Activities();
            myActivity.practiceID = in_practiceID;
            myActivity.sequence = in_sequence;
            myActivity.activity = in_activity;
            myActivity.activityCHN = in_activityCHN;
            myActivity.activityOwner = in_activityOwner;
            myActivity.activityReviewer = in_activityReviewer;
            myActivity.frequency = in_frequency;

            DAL.Activities optActivity = new DAL.Activities();
            bool ok = optActivity.Add(myActivity);
            if (!ok) return 3;
            else return 1;
        }

        /// <summary>
        /// Update the Activity
        /// Return: 1.Success
        ///         2.DAL Error
        /// </summary>
        public int UpdateActivity()
        {
            Model.Activities myActivity = new Model.Activities();
            myActivity.practiceID = this._practiceid;
            myActivity.sequence = this._sequence;
            myActivity.activity = this._activity;
            myActivity.activityCHN = this._activitychn;
            myActivity.activityOwner = this._activityowner;
            myActivity.activityReviewer = this._activityreviewer;
            myActivity.frequency = this._frequency;

            DAL.Activities optActivity = new DAL.Activities();
            bool ok = optActivity.Update(myActivity);
            if (!ok) return 2;
            else return 1;
        }

        /// <summary>
        /// Delete one Activity
        /// Return: 2.Not Exist Activity
        ///         3.Error in deleting the relationship with GoalProcess
        ///         4.Error in deleting the relationship with GoalIT
        ///         5.Error in changing the Practice's processID into EMPTY
        ///         1.Success
        /// </summary>
        public static int DeleteActivity(string in_practiceID, int in_sequence)
        {
            return 0;
        }

        #endregion ActivityFuntion

        #region Activity-Supporting

        /// <summary>
        /// Get the number of Supporting to this Activity
        /// </summary>
        public int CountSupporting()
        {
            DAL.SupportingTemplate optSupporting = new DAL.SupportingTemplate();
            return optSupporting.GetRecordCount("practiceID='" + this._practiceid + "' and sequence=" + this._sequence + " ");
        }

        /// <summary>
        /// Get the list of Supportings
        /// Parameters: 1.in_orderBy:"supportingID","updatedTime","supportingDesigner","supportingOwner","supportingReviewer","frequency","workingHours"
        /// </summary>
        public List<Supporting> Supportings(string in_orderBy, int in_startIndex, int in_endIndex)
        {
            DAL.SupportingTemplate optSupporting = new DAL.SupportingTemplate();
            DataSet ds = optSupporting.GetListByPage("practiceID='" + this._practiceid + "' and sequence=" + this._sequence + " ", in_orderBy, in_startIndex, in_endIndex);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<Supporting> returnSupportings = new List<Supporting>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Supporting newSupporting = new Supporting(ds.Tables[0].Rows[i]["supportingID"].ToString());
                returnSupportings.Add(newSupporting);
            }

            return returnSupportings;
        }

        /// <summary>
        /// Refer to Create a Supporting
        /// 
        /// Return  1.Success
        ///         2.Id already existed
        ///         3.DAL Error
        /// </summary>
        public int AddSupporting(string in_supportingID, string in_supportingName, string in_supportingComment, string in_supportingLink, string in_supportingDesigner, string in_supportingOwner, string in_supportingReviewer, int in_frequency, int in_workingHours)
        {
            return Supporting.CreateSupportingTemplate(this._practiceid, this._sequence, in_supportingID, in_supportingName, in_supportingComment, in_supportingLink, in_supportingDesigner, in_supportingOwner, in_supportingReviewer, in_frequency, in_workingHours);
        }

        /// <summary>
        /// Refer to Delete a Supporting
        /// 
        /// Return: 2.Not Exist Supporting Template
        ///         3.DAL Error in deleting the supporting template
        ///         1.Success
        /// </summary>
        public int RemoveSupporting(string in_supportingID)
        {
            return Supporting.DeleteSupportingTemplate(in_supportingID);
        }

        #endregion Activity-Supporting

        #region Activity-TimeAlarm

        /// <summary>
        /// Get the number of TimeAlarm that belong to this Activity
        /// </summary>
        public int CountTimeAlarm()
        {
            DAL.TimeAlarm optTA = new DAL.TimeAlarm();
            return optTA.GetRecordCount("practiceID='" + this._practiceid + "' and sequence=" + this._sequence + " ");
        }

        /// <summary>
        /// Get the list of TimeAlarm
        /// Parameters: 1.in_orderBy:"practiceID","sequence","alarmDeadline","alarmStatus"
        /// </summary>
        public List<TimeAlarm> TimeAlarms(string in_orderBy, int in_startIndex, int in_endIndex)
        {
            DAL.TimeAlarm optTA = new DAL.TimeAlarm();
            DataSet ds = optTA.GetListByPage("practiceID='" + this._practiceid + "' and sequence=" + this._sequence + " ", in_orderBy, in_startIndex, in_endIndex);
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<TimeAlarm> returnTimeAlarms = new List<TimeAlarm>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Guid temp_id = new Guid(ds.Tables[0].Rows[i]["_id"].ToString());
                TimeAlarm newTimeAlarm = new TimeAlarm(temp_id);
                returnTimeAlarms.Add(newTimeAlarm);
            }

            return returnTimeAlarms;
        }        
        
        #endregion Activity-TimeAlarm

        /*仍然缺乏Activity的删除*/
    }
}
